package com.hlz.rabbit.mq.pubsub;

import com.hlz.rabbit.mq.util.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 发布订阅模式的生产者
 *
 * @author Hongliang Zhu
 * @create 2022-11-20 17:00
 */
public class ProducerFroPubSub {

    public static void main(String[] args) {
        try (Channel channel = RabbitMqUtils.getChannel()) {

            //  定义交换机
            channel.exchangeDeclare("test-fanout", BuiltinExchangeType.FANOUT);
            // 定义队列
            channel.queueDeclare("test-fanout-queue1", false, false, false, null);
            channel.queueDeclare("test-fanout-queue2", false, false, false, null);
            // 队列绑定交换机
            channel.queueBind("test-fanout-queue1", "test-fanout", "");
            channel.queueBind("test-fanout-queue2", "test-fanout", "");

            // 发送消息
            RabbitMqUtils.console(channel, "test-fanout", "");

        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
